﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：曾洋
// 日 期：2012-1-3 16:11:29
// 描 述：私海信息处理
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.Common;
using ZX.DBModule;
using ZX.BLL.Extend;
using System.Text;
using ZX.EHR.Library;
using ZX.CRM.Bussiness;
using ZX.SYS.Library;
using ZX.CRM.Library;

namespace ZX.WebApp.Crm.Customer
{
    public partial class ListCustomer : ListPage
    {
        BTCrmCustomer bll = new BTCrmCustomer();
        string strNoData = "";
        public string fcid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            fcid = Request.QueryString["functionID"] == null ? "" : Request.QueryString["functionID"];
            if (!IsPostBack)
            {
                UserPostDept();                
            }
        }
        /// <summary>
        /// 获取职位树
        /// </summary>
        public void UserPostDept()
        {
            DatUser du = (DatUser)System.Web.HttpContext.Current.Session["User"]; //用户ID
            if (du != null)
            {
                BaseBussnies deptUserPost = new BaseBussnies();
                deptUserPost.entity = new V_DatUserPostDept();
                IList<V_DatUserPostDept> IDatUserPostDept = deptUserPost.GetEntitys<V_DatUserPostDept>("Userid='" + du.UserID.ToString() + "'");
                if (IDatUserPostDept != null)
                {
                    if (IDatUserPostDept.Count > 0)
                    {
                        TreeNode tnParent = new TreeNode();
                        tnParent.Text = "<span>中旭股份</span>";
                        tnParent.Value = "001";
                        tvGsInfo.Nodes.Add(tnParent);
                        //_strHtml.Append("<div  id='menuBar' style='float:left;min-width:133px;min-height:450px'><ul id='menu0' class='menuList'>");
                        for (int i = 0; i < IDatUserPostDept.Count; i++)
                        {
                            IList<DatDepartment> IDatDepartment = bll.GetDatDepartment();
                            if (IDatDepartment != null)
                            {
                                TreeNode tn = new TreeNode();

                                tn.Text = "<span>" + IDatUserPostDept[i].DeptName + "</span>";
                                tn.Value = IDatUserPostDept[i].DeptID.ToString();
                                tn.ToolTip = IDatUserPostDept[i].DeptName;
                                tn.SelectAction = TreeNodeSelectAction.None;
                                tnParent.ChildNodes.Add(tn);
                                //_strHtml.Append("<a href='#@" + IDatUserPostDept[i].DeptID + "'>" + IDatUserPostDept[i].DeptName + "</a>");
                                //_strHtml.Append("<h5 id='tree" + i + "' style='background:url(../../images/menu_Main.gif) 2px 0 no-repeat;'>" + IDatUserPostDept[i].DeptName + "</h5>");

                                //加入树的第一层
                                //AddNode(IDatDepartment, IDatUserPostDept[i].DeptID, _strHtml);
                                AddNode(IDatDepartment, IDatUserPostDept[i].DeptID, tn);
                            }
                        }
                        if (tvGsInfo != null && tvGsInfo.ExpandDepth > 2)
                        {
                            tvGsInfo.ExpandDepth = 1;
                        }
                        //_strHtml.Append("</ul></div>");
                    }
                }
                else
                {
                    strNoData = "0";
                    tvGsInfo.Visible = false;
                    //_strHtml.Append("<div  id='menuBar' style='float:left;min-width:133px;min-height:450px'>");
                    //_strHtml.Append("<font style='color: Red;'>没有用户数据.</font>");
                    //_strHtml.Append("</div>");
                }
            }
        }
        int menu = 0;
        private void AddNode(IList<DatDepartment> list, Guid? key, TreeNode tv)
        {
            menu = menu + 1;
            var detail = list.Where(p => p.ParentID == key).ToList();
            if (detail.Count > 0)
            {

                BaseBussnies deptUserPost = new BaseBussnies();
                deptUserPost.entity = new V_UserEmp();
                IList<V_UserEmp> IUserEmp = deptUserPost.GetEntitys<V_UserEmp>("deptid='" + key.ToString() + "'");
                if (IUserEmp != null)
                {
                    if (IUserEmp.Count > 0)
                    {
                        for (int i = 0; i < IUserEmp.Count; i++)
                        {
                            TreeNode tn = new TreeNode();
                            tn.Text = "<span><a target=\"mainFrame\" title=\"" + IUserEmp[i].Name + "\" href=\"ListPC.aspx?functionID=" + Request.QueryString["functionID"] + "&treeId=" + IUserEmp[i].UserID + "\">" + IUserEmp[i].Name + "</a></span>";
                            tn.Value = IUserEmp[i].UserID.ToString();
                            tv.ChildNodes.Add(tn);
                            //sb.Append("<li>");
                            //sb.Append("<a href=\"ListCustomer.aspx?functionID=" + Request.QueryString["functionID"] + "&treeId=" + IUserEmp[i].UserID + "\">" + IUserEmp[i].Name + "</a>");
                            //sb.Append("</li>");
                        }
                    }
                }
                foreach (DatDepartment v in detail)
                {
                    if (v.ParentID == key)
                    {
                        TreeNode tn = new TreeNode();

                        tn.Text = "<span>" + v.DeptName + "</span>";
                        tn.Value = v.DeptID.ToString();
                        tn.ToolTip = v.DeptName;
                        tn.SelectAction = TreeNodeSelectAction.None;
                        tv.ChildNodes.Add(tn);
                        // tnParent.ChildNodes.Add(tn);
                        //sb.Append("<li>");
                        //sb.Append("<h5 class='h5Normal' onclick='showMenu(" + menu + ")'>" + v.DeptName + "</h5>");
                        //sb.Append("<ul id='menu'" + menu + " class='menuList'>");
                        AddNode(list, v.DeptID, tn); //sb.Append("</ul>");sb.Append("</li>"); 
                    }
                }

            }
            else
            {

                BaseBussnies deptUserPost = new BaseBussnies();
                deptUserPost.entity = new V_UserEmp();
                IList<V_UserEmp> IUserEmp = deptUserPost.GetEntitys<V_UserEmp>("deptid='" + key.ToString() + "'");
                if (IUserEmp != null)
                {
                    if (IUserEmp.Count > 0)
                    {
                        for (int i = 0; i < IUserEmp.Count; i++)
                        {
                            TreeNode tn = new TreeNode();
                            tn.Text = "<span><a target=\"mainFrame\" title=\"" + IUserEmp[i].Name + "\" href=\"ListPC.aspx?functionID=" + Request.QueryString["functionID"] + "&treeId=" + IUserEmp[i].UserID + "\">" + IUserEmp[i].Name + "</a></span>";
                            tn.Value = IUserEmp[i].UserID.ToString();
                            tv.ChildNodes.Add(tn);
                            // tv.ChildNodes.Add(tn);
                            //sb.Append("<li>");
                            //sb.Append("<a href=\"ListCustomer.aspx?functionID="+Request.QueryString["functionID"]+"&treeId=" + IUserEmp[i].UserID + "\">" + IUserEmp[i].Name + "</a>");
                            //sb.Append("</li>");
                        }
                    }

                }

            }
        }

        public string ReturnNoData
        { get { return strNoData; } }
    }
}